package com.szeldon.meteodroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Main activity that gives a possibility to choose which meteorogram to show.
 * 
 * @author szeldon
 * 
 */
public class MainActivity extends Activity implements OnClickListener {

    private String cityName = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.um_model_button);
        button.setOnClickListener(this);
    }

    /** Listener for buttons' clicks. */
    @Override
    public void onClick(View view) {

        if (!isSdPresent()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("You need to have an external storage (SD card for example) to use this application.")
                    .setCancelable(false);
            AlertDialog alert = builder.create();
            alert.show();
            return;
        }

        // acts depending on which button was pushed
        Button sourceButton = (Button) view;
        int sourceButtonId = sourceButton.getId();
        switch (sourceButtonId) {
        case R.id.um_model_button: {

            // String meteogramSite = MeteoPlHelper.requestUmModelMeteogram("2010062600", 406, 250, "pl", "Warszawa");
            //
            // if (meteogramSite != null) {
            // sendShowMeteogramIntent(meteogramSite);
            // } else {
            // Log.e("MeteoPl.", "No meteogram downloaded.");
            // }
            sendShowMeteogramIntent();
            break;
        }
        case R.id.coamps_model_button: {
            break;
        }
        default: {
            throw new IllegalStateException("Wrong state of the program, because id of source button was "
                    + sourceButtonId);
        }
        }
    }

    private void sendShowMeteogramIntent() {
        Log.i("MeteoPl:", "Seding meteorogram to viewer.");

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
        intent.setClassName("com.szeldon.meteodroid", MeteogramActivity.class.getName());

        intent.putExtra(MeteoPlHelper.MODEL_TYPE_ARGUMENT_KEY, MeteoPlHelper.UM_MODEL_TYPE_ARGUMENT_VALUE);
        intent.putExtra(MeteoPlHelper.FORECAST_DATE_ARGUMENT_KEY, "2010062712");
        intent.putExtra(MeteoPlHelper.ROW_ARGUMENT_KEY, 406);
        intent.putExtra(MeteoPlHelper.COLUMN_ARGUMENT_KEY, 250);
        intent.putExtra(MeteoPlHelper.LANGUAGE_ARGUMENT_KEY, "pl");
        intent.putExtra(MeteoPlHelper.CITY_NAME_ARGUMENT_KEY, "Warszawa");

        startActivity(intent);

    }

    public static boolean isSdPresent() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
    // TODO: add menu with about and settings. settings will give an option to set a city etc.
}